package controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import model.EmailDAO;
import model.Model;

import databean.Email;
import databean.User;


public class HomeAction extends Action {

	private EmailDAO	emailDAO;

	public HomeAction(Model model) {
		emailDAO = model.getEmailDAO();
	}

	public String getName() { return "home.do"; }

	public String perform(HttpServletRequest request,HttpServletResponse response) {
        // Set up the errors list
        List<String> errors = new ArrayList<String>();
        request.setAttribute("errors",errors);
        
        HttpSession session = request.getSession();
        
        
		try {
	        
			if (session.getAttribute("user") == null) {
				return "user-login.do";
			}
			
			request.setAttribute("user",(User) request.getSession(false).getAttribute("user"));
			
			
			emailDAO.addDummyData();
			
			
			User user = (User) request.getSession(false).getAttribute("user");
			ArrayList<Email> newEmails = emailDAO.getUserEmails(user.getUserID());
			

	        request.setAttribute("emails",newEmails);
	        
	        return "home.jsp";
	        
	        
        } catch (Exception e) {
        	errors.add(e.getMessage());
        	return "user-error-list.jsp";
        }
        
    }
}



